﻿package org.FaPe3D.core.parsers.casting
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	import org.FaPe3D.Adapter;
	import org.FaPe3D.engine.materials.*;
	import org.FaPe3D.engine.materials.shaded.*;
	import org.FaPe3D.engine.materials.shaders.*;
	import org.FaPe3D.engine.materials.utils.*;
	import org.FaPe3D.utils.*;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	
	public class Materials
	{
		public function cast(material:Object = null, param: Object = null):*
		{
			var result:*;
			
			switch (material.type)
			{
				case 'ColorMaterial':
					result = new FPColorMaterial(material.property,
												InitialValues.check(param.color,0xFF00FF),
												InitialValues.check(param.alpha,1),
												InitialValues.check(param.interactive,false));
				break;
				case 'MovieAssetMaterial':
				trace(param.linkageID);
					result = new FPMovieAssetMaterial(material.property,
													  InitialValues.check(param.linkageID,''),
													  InitialValues.check(param.transparent,false),
												      InitialValues.check(param.animated,false),
												      InitialValues.check(param.createUnique,false),
												      InitialValues.check(param.precisee,false));
				break;
				case 'MovieMaterial':
					result = new FPMovieMaterial(material.property,
												 InitialValues.check(param.movieAsset,new MovieClip()) as DisplayObject,
												 InitialValues.check(param.transparent,false),
											     InitialValues.check(param.animated,false),
											     InitialValues.check(param.precisee,false),
											     InitialValues.check(param.rect,null));
				break;
				case 'MagicalMaterial':
					result = new FPMagicalMaterial(material.property,
												 InitialValues.check(param.url,''),
												 InitialValues.check(param.transparent,false),
											     InitialValues.check(param.animated,false),
											     InitialValues.check(param.precisee,false),
											     InitialValues.check(param.rect,null));
				break;
				case 'BitmapFileMaterial':
					result = new FPBitmapFileMaterial(material.property,
												 	  InitialValues.check(param.url,''),
											     	  InitialValues.check(param.precisee,false));	     	  
				break;
				case 'FlatShader':
					var light:* = new PointLight3D(true);
					var shader:FPFlatShader = new FPFlatShader(material.property,
												InitialValues.check(param.light,light),
												InitialValues.check(param.diffuse,0xFFFFFF),
												InitialValues.check(param.ambient,0x000000)); 
					result = new ShadedMaterial(new FPMovieAssetMaterial(null,'asset'),shader);
				break;
				default:
					result = new FPColorMaterial(material.property,
												InitialValues.check(param.color,0xFF00FF),
												InitialValues.check(param.alpha,1),
												InitialValues.check(param.interactive,false));
			}
			RegisterProperties.register(result,material.property);
			return result;
		}
	}
}